package com.ruoyi.template.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.template.domain.VtsTemplateCodeDept;
import com.ruoyi.template.mapper.VtsTemplateCodeDeptMapper;
import com.ruoyi.template.service.IVtsTemplateCodeDeptService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.ruoyi.common.helper.LoginHelper.getUserId;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-22
 */
@RequiredArgsConstructor
@Service
public class VtsTemplateCodeDeptServiceImpl implements IVtsTemplateCodeDeptService {

    private final VtsTemplateCodeDeptMapper baseMapper;




    private final SysUserMapper userMapper;
    @Override
    public R<Void> publish(Long formId) {
        //获取当前登录用户信息
        SysUser user = userMapper.selectUserById(getUserId());
        Long deptId = user.getDeptId();
        //查看是否有部门
        if(deptId!=null){
            //有部门
            //添加
            //存入数据库
            boolean result= baseMapper.insert(new VtsTemplateCodeDept(deptId,formId))>0;
            return result ? R.ok("发表成功"): R.fail("插入数据异常");
        }

        return R.fail("该用户没有部门，发布失败");
    }

    @Override
    public List<Long> getFormIds() {
        //获取当前登录用户信息
        SysUser user = userMapper.selectUserById(getUserId());
        Long deptId = user.getDeptId();
        QueryWrapper<VtsTemplateCodeDept> query = Wrappers.query();
        //查看是否有部门
        List<Long> formIds =new ArrayList();
        if(deptId!=null){
            //有部门，查询当前部门的  自定义表单
            query.eq("dept_id",deptId);
            List<VtsTemplateCodeDept> vtsTemplateCodeDepts = baseMapper.selectList(query);
            //添加到结果
            for (VtsTemplateCodeDept vtsTemplateCodeDept : vtsTemplateCodeDepts) {
                formIds.add(vtsTemplateCodeDept.getFormId());
            }
        }
        return formIds;
    }

}
